package com.gang.music.core.intercept;

import com.gang.music.core.aop.Invocation;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestValidator extends BladeValidator {


	/**
	 * 判断是否是ajax请求,如果是就返回518状态码,页面配置了Ajax全局的参数,
	 * 因此会弹出登录超时的提示框,点击后,页面重定向到登录页面
	 *
	 */
	@Override
	protected void doValidate(Invocation inv) {
		
		Object[] args = inv.getArgs();
		HttpServletRequest request = (HttpServletRequest) args[0];
		HttpServletResponse response = (HttpServletResponse) args[1];
		
		String requestType = request.getHeader("X-Requested-With");
		if (StringUtils.isNotEmpty(requestType)
				&& requestType.equalsIgnoreCase("XMLHttpRequest")){
			
			response.setHeader("sessionstatus", "timeout");
			try {
				response.sendError(518, "session timeout.");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
